//
//  LoginViewController.m
//  QuickReadComic
//
//  Created by 苗钧豪 on 16/8/27.
//  Copyright © 2016年 苗钧豪. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginTextView.h"
#import "Masonry.h"
#import "JHMacro.h"
#import "VerifyViewController.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "ZWYNetTool.h"
#import "MineModel.h"
#import "JHAccountTool.h"
#import "SyntonyViewController.h"

@interface LoginViewController ()
@property(nonatomic,strong)LoginTextView *myView;// 自定义View
@property(nonatomic,strong)UIButton *LoginBtn;// 登录按钮
@property(nonatomic,strong)UIButton *RegisterBtn;// 注册按钮
@property(nonatomic,strong)UILabel *Otherlabel;// 分割线
@property(nonatomic,strong)UIButton *backBtn;// 返回按钮
@property(nonatomic,strong)UIButton *FoundBtn;// 找回密码按钮
@property(nonatomic,strong)MineModel *model;
@property(nonatomic,copy)NSString *string;
@end

@implementation LoginViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置导航栏背景图片为空
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    // 设置导航栏阴影图片为空
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    // 设置导航栏不显示
    self.navigationController.navigationBar.hidden = YES;
    // 背景颜色
    self.view.backgroundColor = [UIColor colorWithRed:1.000 green:0.827 blue:0.000 alpha:1.000];
    // 初始化自定义View
    self.myView = [[LoginTextView alloc]initWithFrame:CGRectMake(50, 130, SCREEN_WIDTH - 100, 220)];
    self.myView.loginView = self;
    // 登录按钮
    self.LoginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _LoginBtn.frame = CGRectMake(50, 370, SCREEN_WIDTH - 100, 40);
    [_LoginBtn setTitle:@"登录" forState:(UIControlStateNormal)];
    [_LoginBtn setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    _LoginBtn.backgroundColor = [UIColor colorWithWhite:0.171 alpha:1.000];
    [_LoginBtn addTarget:self action:@selector(NetWork) forControlEvents:(UIControlEventTouchUpInside)];
    // 注册按钮
    self.RegisterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _RegisterBtn.frame = CGRectMake(SCREEN_WIDTH / 2 - 50, 450, 100, 30);
    [_RegisterBtn setTitle:@"立即注册" forState:(UIControlStateNormal)];
    [_RegisterBtn addTarget:self action:@selector(pushNextView) forControlEvents:(UIControlEventTouchUpInside)];
    // 加粗字体
    _RegisterBtn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold"size:20];
    [_RegisterBtn setTitleColor:[UIColor brownColor] forState:(UIControlStateNormal)];
    [self.view addSubview:self.RegisterBtn];
    // 分割线Label
    self.Otherlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 520, SCREEN_WIDTH, 21)];
    [_Otherlabel setTextColor:[UIColor grayColor]];
    _Otherlabel.text = @"————— 其他方式登录 —————";
    _Otherlabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_Otherlabel];
    // 返回按钮
    self.backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _backBtn.frame = CGRectMake(10, 20, 20, 20);
    [_backBtn setImage:[UIImage imageNamed:@"IMG_0418"] forState:(UIControlStateNormal)];
    [_backBtn addTarget:self action:@selector(back) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:self.backBtn];
    // 找回密码按钮
    self.FoundBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _FoundBtn.frame = CGRectMake(SCREEN_WIDTH - 100, 35, 100, 10);
    [_FoundBtn setTitle:@"忘记密码" forState:(UIControlStateNormal)];
    [_FoundBtn setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    [_FoundBtn addTarget:self action:@selector(pushNextView) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:self.FoundBtn];
    // 登录按钮倒圆角
    CALayer *layer2 = self.LoginBtn.layer;
    [layer2 setMasksToBounds:YES];
    [layer2 setCornerRadius:10];
    // 添加到self.view上
    [self.view addSubview:self.myView];
    [self.view addSubview:self.LoginBtn];
    
    UIButton *QQBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    QQBtn.frame = CGRectMake(80, self.view.bounds.size.height - 100, 80, 80);
    [QQBtn setImage:[UIImage imageNamed:@"IMG_0421"] forState:(UIControlStateNormal)];
    [self.view addSubview:QQBtn];
    
    UIButton *WeiXinBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    WeiXinBtn.frame = CGRectMake(self.view.bounds.size.width - 160, self.view.bounds.size.height - 100, 80, 80);
    [WeiXinBtn setImage:[UIImage imageNamed:@"IMG_0422"] forState:(UIControlStateNormal)];
    [self.view addSubview:WeiXinBtn];
}
// 点击登录
- (void)NetWork {
    [self.view endEditing:YES];
    [ZWYNetTool POST:@"http://api.kuaikanmanhua.com/v1/phone/signin" andBody:[NSString stringWithFormat:@"password=%@&phone=%@",self.myView.passWordField.text,self.myView.nickNameField.text] andBodyStyle:ZWYBodyString andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        self.model = [MineModel modelWithDic:result[@"data"]];
        if ([result[@"message"] isEqualToString:@"OK"]) {
            self.lastVC.nameStr = self.model.nickname;
            self.lastVC.imageStr = self.model.avatar_url;
            [self.lastVC refreshLabel];
            // 将用户名存储到本地
            [JHAccountTool saveAccount:[NSArray arrayWithObjects:self.model.nickname,self.model.avatar_url, nil]];
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            NSInteger num = [user integerForKey:@"isLogin"];
            num = 1;
            [user setInteger:num forKey:@"isLogin"];
            [self dismissViewControllerAnimated:YES completion:nil];
        } else {
            
        }
    } andFailureBlock:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
}
// 模态返回方法
- (void)back {
    [self dismissViewControllerAnimated:YES completion:nil];
}
// push跳转到注册、找回密码界面
- (void)pushNextView {
    SyntonyViewController *vc = [[SyntonyViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
